﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace FlanProject.Combats
{
    public class CombatEffect
    {
        #region Attributes

        #region Font

        private Color fontColor = Color.White;
        public static SpriteFont Font { get; set; }

        #endregion

        #region Text

        private int damage;
        private Vector2 textOrigin = Vector2.Zero;

        public String Text { get; set; }
        public Vector2 OriginalPosition { get; set; }
        public Vector2 Position { get; set; }

        #endregion

        #region Rise

        private const int risePerSecond = 100;
        private const int riseMaximum = 10;

        public float Rise { get; set; }
        public bool IsRiseComplete { get; set; }

        #endregion

        #endregion

        public CombatEffect(Vector2 position, int damageReceived)
        {
            OriginalPosition = position;
            damage = damageReceived;
        }

        public static void LoadContent(ContentManager contentManager)
        {
            Font = contentManager.Load<SpriteFont>(@"Sprites\Fonts\CombatFont");
        }

        public void Update(GameTime gameTime)
        {
            if (!IsRiseComplete)
            {
                Rise += ((float)risePerSecond * (float)gameTime.ElapsedGameTime.TotalSeconds);

                if (Rise > riseMaximum)
                {
                    Rise = riseMaximum;
                    IsRiseComplete = true;
                }

                Position = new Vector2(OriginalPosition.X, OriginalPosition.Y - Rise);
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (spriteBatch == null)
                throw new ArgumentNullException("No SpriteBatch.");

            Text = damage.ToString();

            if (damage > 0)
            {
                fontColor = Color.Red;
            }
            else if (damage < 0)
            {
                fontColor = Color.Green;
            }
            else
            {
                Text = "Block !";
            }

            var textSize = Font.MeasureString(Text);
            textOrigin = new Vector2((float)Math.Ceiling(textSize.X / 2f), (float)Math.Ceiling(textSize.Y / 2f));

            spriteBatch.DrawString(Font, Text, Position, fontColor, 0f, new Vector2(textOrigin.X, textOrigin.Y), 1f,
                SpriteEffects.None, 0.2f * (float)Rise / 200f);
        }
    }
}
